package com.nbcb.fund.service.impl;

import com.nbcb.fund.common.exception.exception.BusinessException;
import com.nbcb.fund.common.util.AuthUtil;
import com.nbcb.fund.entity.code.FundCode;
import com.nbcb.fund.entity.po.*;
import com.nbcb.fund.entity.vo.FundDetailVo;
import com.nbcb.fund.entity.vo.MyFundVo;
import com.nbcb.fund.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ChinamutualFundServiceImpl implements ChinamutualFundService {

    @Resource
    MyFundKeyService myFundKeyService;

    @Resource
    ChinamutualFundDescriptionService chinamutualFundDescriptionService;

    @Resource
    ChinamutualFundManagerService chinamutualFundManagerService;

    @Resource
    ChinamutualFundNavService chinamutualNavService;

    @Override
    public List<MyFundVo> listMyFund() {
        FundUserPo currentUser = AuthUtil.getCurrentUser();
        List<MyFundKeyPo> myFundKeyPoList = myFundKeyService.selectByUserId(currentUser.getUserId());
        List<String> windCodes = new ArrayList<>();
        for (MyFundKeyPo myFundKeyPo : myFundKeyPoList) {
            windCodes.add(myFundKeyPo.getFundCode());
        }
        if (windCodes.size() == 0){
            return new ArrayList<>();
        }
        return chinamutualFundDescriptionService.selectByWindCodes(windCodes);
    }

    @Override
    public List<MyFundVo> listAllFund() {
        return chinamutualFundDescriptionService.selectByWindCodes(null);
    }

    @Override
    public MyFundVo getByWindCode(String fInfoWindcode) {
        ChinamutualFundDescriptionPo chinamutualFundDescriptionPo = chinamutualFundDescriptionService.getByWindCode(fInfoWindcode);
        MyFundVo myFundVo = new MyFundVo();
        BeanUtils.copyProperties(chinamutualFundDescriptionPo, myFundVo);
        return myFundVo;
    }

    @Override
    public int addMyFund(String fInfoWindcode) {
        FundUserPo currentUser = AuthUtil.getCurrentUser();
        if (null == currentUser) {
            throw new BusinessException(FundCode.UN_LOGIN);
        }
        String userId = currentUser.getUserId();
        MyFundKeyPo myFundKeyPo = new MyFundKeyPo();
        myFundKeyPo.setUserId(userId);
        myFundKeyPo.setFundCode(fInfoWindcode);
        MyFundKeyPo myFundKeyPo2 = myFundKeyService.selectByUserIdAndFundCode(myFundKeyPo);
        if (myFundKeyPo2 != null) {
            throw new BusinessException("该基金已经添加，不可重复添加");
        }
        return myFundKeyService.addMyFund(myFundKeyPo);
    }

    @Override
    public int deleteMyFund(String fInfoWindcode) {
        FundUserPo currentUser = AuthUtil.getCurrentUser();
        if (null == currentUser) {
            throw new BusinessException(FundCode.UN_LOGIN);
        }
        String userId = currentUser.getUserId();
        MyFundKeyPo myFundKeyPo = new MyFundKeyPo();
        myFundKeyPo.setUserId(userId);
        myFundKeyPo.setFundCode(fInfoWindcode);
        return myFundKeyService.deleteMyFund(myFundKeyPo);
    }

    @Override
    public FundDetailVo getFundDetailByWindCode(String fInfoWindcode) {
        FundDetailVo fundDetailVo = new FundDetailVo();
        //基金基础信息
        ChinamutualFundDescriptionPo chinamutualFundDescriptionPo = chinamutualFundDescriptionService.getByWindCode(fInfoWindcode);
        fundDetailVo.setChinamutualFundDescriptionPo(chinamutualFundDescriptionPo);
        //基金管理人信息
        ChinamutualFundManagerPo chinamutualFundManagerPo = chinamutualFundManagerService.getByWindCode(fInfoWindcode);
        fundDetailVo.setChinamutualFundManagerPo(chinamutualFundManagerPo);
        //基金净值信息
        ChinamutualFundNavPo chinamutualFundNavPo = chinamutualNavService.getOneByWindCode(fInfoWindcode);
        fundDetailVo.setChinamutualFundNavPo(chinamutualFundNavPo);
        //计算基金最新的涨跌幅
        double zhngdiefu = chinamutualNavService.getZhangDieFuByWindCode(fInfoWindcode);
        fundDetailVo.setZhangdifu(zhngdiefu);
        return fundDetailVo;
    }

    @Override
    public List<ChinamutualFundNavPo> fundHistoryNav(String fInfoWindcode) {
        List<ChinamutualFundNavPo> list = chinamutualNavService.listByHistoryNav(fInfoWindcode);
        return list;
    }


}
